'use strict';

module.exports = (app, conf, load) => {
   if (conf.clickHouse) {
      const { ClickHouse } = require('clickhouse');

      try {
         app.fs = app.fs || {}
         app.fs.clickHouse = {}
         const { url, port, user, password, db = [], debug = false } = conf.clickHouse
         for (let d of db) {
            if (d.name && d.db) {
               load.text = `加载 clickHouse ${d.name}`
               app.fs.clickHouse[d.name] = new ClickHouse({
                  url: url,
                  port: port,
                  debug: debug === 'false' ? false : debug,
                  format: "json",
                  usePost: true,
                  basicAuth: user && password ? {
                     username: user,
                     password: password,
                  } : null,
                  config: {
                     database: d.db,
                  },
               })
            } else {
               throw 'conf.clickHouse 参数错误！'
            }
         }
      } catch (error) {
         console.error(error)
         process.exit(-1);
      }
   }
};
